package com.weakteam.orderonline.administratorconsole.gwt.client.widget;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
import com.weakteam.orderonline.administratorconsole.gwt.client.design.MenuEditPage;
import com.weakteam.orderonline.administratorconsole.gwt.client.domain.Menu;
import com.weakteam.orderonline.administratorconsole.gwt.client.util.ConsoleLog;

import java.util.Iterator;
import java.util.List;


/**
 *
 * @author Nail
 */
public class OrderMenuDraggableBox extends Label {

    private Menu menu;

    private Boolean active;

    private List<OrderMenuDraggableBox> boxes;

    public Boolean isActive() {
        return active;
    }

    public OrderMenuDraggableBox() {
        this.setStyleName("order-draggable-widget");
        this.active = false;
        this.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                ConsoleLog.getInstance().debug(OrderMenuDraggableBox.this.getText());
            }
        });
    }

    public void init(Menu menu, List<OrderMenuDraggableBox> boxes){
        this.boxes = boxes;
        this.menu = menu;
        this.setText(menu.getName());
    }

    public Menu getMenu(){
        return this.menu;
    }

    public void setActive() {
        for (Iterator<OrderMenuDraggableBox> it = boxes.iterator(); it.hasNext();) {
            OrderMenuDraggableBox box = it.next();
            if (box.isActive()) {
                box.removeStyleName("order-draggable-widget-active");
                MenuEditPage.getInstance().save(box);
                box.active = false;
            }
        }
        this.addStyleName("order-draggable-widget-active");
        this.active = true;
        MenuEditPage.getInstance().setActive(this);
    }

    public void setEnabled(Boolean enabled) {
        if (enabled) {
            this.removeStyleName("order-draggable-widget-disabled");
        } else {
            this.addStyleName("order-draggable-widget-disabled");
        }
    }

}